package com.wxw.getapp.adapter

import android.app.AlertDialog
import android.content.Context
import android.graphics.Bitmap
import android.graphics.Canvas
import android.util.Log
import android.view.View
import android.view.ViewGroup
import com.safframework.log.L
import com.wxw.getapp.R
import com.wxw.getapp.bean.AppInfo
import android.graphics.drawable.Drawable
import android.graphics.PixelFormat
import android.widget.*
import android.widget.Toast
import android.content.DialogInterface
import com.wxw.getapp.MainActivity
import com.wxw.getapp.utils.GetApk
import android.support.v4.content.ContextCompat.startActivity
import android.content.Intent
import android.net.Uri
import java.io.File


/**
 * Created by Administrator on 2018/11/14.
 */
class AppAdapter(context: Context) : BaseAdapter() {

    var context = context

    var items = ArrayList<String>()
     var applistData:List<AppInfo> = ArrayList<AppInfo>()

    fun setBeanData(applistData:List<AppInfo>) {
        this.applistData = applistData
        notifyDataSetChanged()
    }

    override fun getView(p0: Int, p1: View?, p2: ViewGroup?): View {
        var viewHolder: ViewHolder
        var view: View

        if (p1 == null) {
            view = View.inflate(context, R.layout.list_items, null)
            viewHolder = ViewHolder(view)
            view.setTag(viewHolder)
        } else {
            view = p1
            viewHolder = view.getTag() as ViewHolder
        }

//        Glide.with(context)
//                .load(applistData[p0].drawable.)
//                .asBitmap()
//                .into(viewHolder.app_img)
        viewHolder.app_img.setImageDrawable(applistData[p0].drawable)
        viewHolder.item_test.text = applistData[p0].appName
        viewHolder.app_dizhi.text = applistData[p0].packageName
        viewHolder.app_btn.setOnClickListener{
//            L.e("出来了EEEEEEEEEEEEEEEEEEEEEE")
//            Toast.makeText(context,applistData[p0].appName,Toast.LENGTH_SHORT).show()
            val builder = AlertDialog.Builder(context)
//            builder.setIcon(R.mipmap.get)
//            builder.setTitle("")
            //    指定下拉列表的显示数据
            val cities = arrayOf("提取","分享")
            //    设置一个下拉的列表选择项
            builder.setItems(cities) { dialog, which ->
                when (which) {

                    0 -> {
                        Toast.makeText(context, "正在提取>>>" + applistData[p0].appName, Toast.LENGTH_SHORT).show()
                        GetApk.backupApp(applistData[p0],context)
                    }
                    1 -> {


                        val apkFile = File(applistData[p0].info.applicationInfo.sourceDir)
                        val intent = Intent()
                        intent.action = Intent.ACTION_SEND
                        intent.type = "*/*"
//                        intent.type = "application/vnd.android.package-archive"
                        intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(apkFile))
                        context.startActivity(intent)
                    }

                }
            }
            builder.show()
        }

        return view
    }

    override fun getItem(p0: Int): Any {
        return items.get(p0)
    }

    override fun getItemId(p0: Int): Long {
        return p0.toLong()
    }

    override fun getCount(): Int {
        return applistData.size
    }

    class ViewHolder(var v: View) {
        var item_test: TextView = v.findViewById(R.id.app_name)
        var app_img: ImageView = v.findViewById(R.id.app_img)
        var app_dizhi: TextView = v.findViewById(R.id.app_dizhi)
        var app_btn: Button = v.findViewById(R.id.app_btn)
    }

    fun drawableToBitmap(drawable: Drawable): Bitmap {
        // 取 drawable 的长宽
        val w = drawable.intrinsicWidth
        val h = drawable.intrinsicHeight

        // 取 drawable 的颜色格式
        val config = if (drawable.opacity != PixelFormat.OPAQUE)
            Bitmap.Config.ARGB_8888
        else
            Bitmap.Config.RGB_565
        // 建立对应 bitmap
        val bitmap = Bitmap.createBitmap(w, h, config)
        // 建立对应 bitmap 的画布
        val canvas = Canvas(bitmap)
        drawable.setBounds(0, 0, w, h)
        // 把 drawable 内容画到画布中
        drawable.draw(canvas)
        return bitmap
    }

}
